from application.models import User
from application.modules.index import index_blue
from flask import render_template, request, jsonify, session


@index_blue.route("/")
def index():
    return render_template('login.html')


@index_blue.route("/login", methods=["POST", "GET"])
def login():
    # if request.method == "GET":
    #     return render_template('login.html')
    dict_req = {'state': "No", 'id': 0}
    username = request.args.get('username')
    password = request.args.get("password")
    remember = request.args.get('remember')
    
    user = User.query.filter_by(username=username).first()
    if user == None:
        return jsonify(dict_req)
    if user.password == password:
        session['id'] = str(user.id)
        dict_req["state"] = "YES"
        dict_req["id"] = user.id
    return jsonify(dict_req)


@index_blue.route("/register")
def register():
    return render_template('register.html')
